#include "graphweekly.h"
#include "ui_graphweekly.h"
#include<QDesktopWidget>
#include<QtSql/QSqlQuery>
#include<QDebug>
#include<QDate>
#include<QMessageBox>
//#include<QFile>
//#include<QTextStream>
//This shows weekly graphical view of expenses
GraphWeekly::GraphWeekly(QDate dt,int wk,QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::GraphWeekly)
{
    ui->setupUi(this);
    date=dt;
    week=wk;

   /* QFile f("C:\\Data\\log1.txt");

    if(f.open(QIODevice::Append))
    {
        QTextStream t(&f);
        t<<"In weekly";
    }*/
    QAction* backAction = new QAction( tr("Back"), this );
    backAction->setSoftKeyRole( QAction::NegativeSoftKey );
    connect(backAction, SIGNAL(triggered()), this, SLOT(back()));
    addAction( backAction );
    QRect q=QApplication::desktop()->availableGeometry(this);

    int margin=0,Awidth=0;
   /* if(q.width()<=q.height())
     {Awidth=q.width();
       margin=q.width()/12.0;//For 240 width ,it is 20
   }
    else
    {
       margin=q.height()/12.0;
       Awidth=q.height();
   }*/
    Awidth=q.width();
    margin=q.width()/12.0;//For 320 width ,it is 26
    if(q.width()>500)
        margin=q.width()/18.0;

    int fontSize=6;
    ui->label_header->setFont(QFont( "lucida", 1.5*fontSize, QFont::Normal, TRUE));
    ui->label_header->setGeometry(margin,q.height()/3.0,q.width(),margin);
    ui->label_header->setVisible(false);
    ui->Vline->setGeometry(2.0*margin,margin,margin,(q.height()-(3*margin)));//Vertical

    ui->Hline->setGeometry((2.5*margin),(q.height()-(2.0*margin)),((q.width())-(3.0*margin)),margin/10.0);//Horizontal
    int avWidth=(Awidth-(ui->Vline->width()*4.0));//For 240, it is 160 available width
    int space=avWidth/40.0;//For 160 available width, it is 4
    int size=(avWidth/7.0)-space;
    int labelSize=2.1*size;
    if(q.width()>500)
        labelSize=1.1*size;
    ui->label_header->setVisible(false);
    int width=(ui->Vline->width()*3.0);

    ui->labelX->setAlignment(Qt::AlignRight);
    ui->labelX->setGeometry(0,(q.height()-(1.6*margin)),(2.5*margin),size);
    ui->labelX->setFont((QFont( "lucida", fontSize, QFont::Normal, TRUE )));
    ui->labelX->setText("Day");

    ui->progressBar1->setGeometry(width,margin,size,(ui->Vline->height()));
    ui->label1->setGeometry(width,(q.height()-(1.8*margin)),size,size);
    ui->label1->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

    width=width+ui->progressBar1->width()+space;
    ui->progressBar2->setGeometry(width,margin,size,(ui->Vline->height()));
    ui->label2->setGeometry(width,(q.height()-(1.8*margin)),size,size);
    ui->label2->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

    width=width+ui->progressBar2->width()+space;
    ui->progressBar3->setGeometry(width,margin,size,(ui->Vline->height()));
    ui->label3->setGeometry(width,(q.height()-(1.8*margin)),size,size);
    ui->label3->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

    width=width+ui->progressBar3->width()+space;
    ui->progressBar4->setGeometry(width,margin,size,(ui->Vline->height()));
    ui->label4->setGeometry(width,(q.height()-(1.8*margin)),size,size);
    ui->label4->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

    width=width+ui->progressBar4->width()+space;
    ui->progressBar5->setGeometry(width,margin,size,(ui->Vline->height()));
    ui->label5->setGeometry(width,(q.height()-(1.8*margin)),size,size);
    ui->label5->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

    width=width+ui->progressBar5->width()+space;
    ui->progressBar6->setGeometry(width,margin,size,(ui->Vline->height()));
    ui->label6->setGeometry(width,(q.height()-(1.8*margin)),size,size);
    ui->label6->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

    width=width+ui->progressBar6->width()+space;
    ui->progressBar7->setGeometry(width,margin,size,(ui->Vline->height()));
    ui->label7->setGeometry(width,(q.height()-(1.8*margin)),size,size);
    ui->label7->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

    ui->Vlabel1->setAlignment(Qt::AlignRight);
    ui->Vlabel2->setAlignment(Qt::AlignRight);
    ui->Vlabel3->setAlignment(Qt::AlignRight);
    ui->Vlabel4->setAlignment(Qt::AlignRight);
    ui->Vlabel5->setAlignment(Qt::AlignRight);

    /*width=width+ui->progressBar7->width();
    ui->labelXT->setFont((QFont( "lucida", fontSize, QFont::Normal, TRUE )));
    ui->labelXT->setGeometry(width,(q.height()-(1.8*margin)),size,size);
    ui->labelXT->setText("X");*/

    ui->Vlabel1->setFont((QFont( "lucida", 5, QFont::Normal, TRUE )));
    ui->Vlabel2->setFont((QFont( "lucida", 5, QFont::Normal, TRUE )));
    ui->Vlabel3->setFont((QFont( "lucida", 5, QFont::Normal, TRUE )));
    ui->Vlabel4->setFont((QFont( "lucida", 5, QFont::Normal, TRUE )));
    ui->Vlabel5->setFont((QFont( "lucida", 5, QFont::Normal, TRUE )));

    ui->Vlabel1->setGeometry(0,margin-0.2*size,labelSize,0.6*size);
    ui->Vlabel2->setGeometry(0,((ui->Vline->height()*0.75)+ margin-0.2*size),labelSize,0.6*size);
    ui->Vlabel3->setGeometry(0,((ui->Vline->height()/2.0)+margin-0.2*size),labelSize,0.6*size);
    ui->Vlabel4->setGeometry(0,((ui->Vline->height()/4.0)+margin-0.2*size),labelSize,0.6*size);
    ui->Vlabel5->setGeometry(0.5*size,(ui->Vline->height()+margin-0.2*size),(labelSize-0.5*size),0.4*size);

    plotGraph();


}

GraphWeekly::~GraphWeekly()
{
    delete ui;
}
void GraphWeekly::back()
{
    (new ExpWeek)->show();
}
void GraphWeekly::plotGraph()
{

    int listSun[7]={0,0,0,0,0,0,0};
    QDate dt=date;
    int i=1,j=1;
    while(i<=date.daysInMonth())
    {
        dt.setDate(date.year(),date.month(),i);

        if(dt.dayOfWeek()==7)
        {
            listSun[j]=i;
            qDebug()<<listSun[j];
            j++;

        }
        i++;
    }
    int NumWeeks=0;
    qDebug()<<"J"<<j;
    if(listSun[j-1]==date.daysInMonth())
    {
        NumWeeks=j-1;
    }
    else
        NumWeeks=j;
    if(listSun[5]==0)
    {
        listSun[5]=date.daysInMonth();
    }
    if(listSun[6]==0)
    {
        listSun[6]=date.daysInMonth();
    }

    if(week==1)
    {
        dayOne=date;
        dayLast.setDate(date.year(),date.month(),listSun[1]);
    }
    else
    {
        dayOne.setDate(date.year(),date.month(),listSun[week-1]+1);
        qDebug()<<week;
        qDebug()<<listSun[week-1];
        dayLast.setDate(date.year(),date.month(),listSun[week]);

    }
    db=QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("finance_db");
    if(!db.open())
    {
        QMessageBox::information(0,"Error !","Error in opning database");

    }
    else
    {
        QSqlQuery qry;
        qry.prepare(QString("select * from expense_table where exp_date>=? and exp_date<=?"));
        qry.addBindValue(dayOne);
        qry.addBindValue(dayLast);
        if(qry.exec())
        {
            int count=0;
            double list[8];
            list[1]=list[2]=list[3]=list[4]=list[5]=list[6]=list[7]=0;
            while(qry.next())
            {
              switch(qry.value(3).toDate().dayOfWeek())
              {
              case 1:list[1]=list[1]+qry.value(1).toDouble();
                     break;
              case 2:list[2]=list[2]+qry.value(1).toDouble();
                     break;
              case 3:list[3]=list[3]+qry.value(1).toDouble();
                     break;
              case 4:list[4]=list[4]+qry.value(1).toDouble();
                     break;
              case 5:list[5]=list[5]+qry.value(1).toDouble();
                     break;
              case 6:list[6]=list[6]+qry.value(1).toDouble();
                     break;
              case 7:list[7]=list[7]+qry.value(1).toDouble();
                     break;
              default:QMessageBox::information(0,"Error!","Wrong day of week");

              }
              count++;
            }
            if(count==0)
            {
                ui->label_header->setVisible(true);
                ui->label_header->setText(tr("No expense record found"));
                ui->Vline->setVisible(false);
                ui->Hline->setVisible(false);
                ui->progressBar1->setVisible(false);
                ui->progressBar2->setVisible(false);
                ui->progressBar3->setVisible(false);
                ui->progressBar4->setVisible(false);
                ui->progressBar5->setVisible(false);
                ui->progressBar6->setVisible(false);
                ui->progressBar7->setVisible(false);
                ui->labelX->setVisible(false);

            }
            else
            {
                double max=0;
                for(int i=1;i<=7;i++)
                {
                    if(list[i]>max)
                        max=list[i];
                }
                ui->progressBar1->setMaximum(max);
                ui->progressBar2->setMaximum(max);
                ui->progressBar3->setMaximum(max);
                ui->progressBar4->setMaximum(max);
                ui->progressBar5->setMaximum(max);
                ui->progressBar6->setMaximum(max);
                ui->progressBar7->setMaximum(max);

                ui->progressBar1->setValue(list[1]);
                ui->progressBar2->setValue(list[2]);
                ui->progressBar3->setValue(list[3]);
                ui->progressBar4->setValue(list[4]);
                ui->progressBar5->setValue(list[5]);
                ui->progressBar6->setValue(list[6]);
                ui->progressBar7->setValue(list[7]);

                ui->label1->setText(tr("M"));
                ui->label2->setText(tr("T"));
                ui->label3->setText(tr("W"));
                ui->label4->setText(tr("T"));
                ui->label5->setText(tr("F"));
                ui->label6->setText(tr("S"));
                ui->label7->setText(tr("Su"));

                int maxi=max;
                qDebug()<<max;
                qDebug()<<maxi;
                ui->Vlabel1->setText(QString("%1").arg(maxi));
                ui->Vlabel2->setText(QString("%1").arg(maxi/4.0));
                ui->Vlabel3->setText(QString("%1").arg(maxi/2.0));
                ui->Vlabel4->setText(QString("%1").arg(maxi*0.75));
                ui->Vlabel5->setText("0");


            }
            db.close();
        }
    }

}
